/**
 * 分页显示数据表格，可以对表格中的数据项进行排序
 */
Ext.namespace("Ext.ux.grid");

Ext.ux.grid.SortGrid = Ext.extend(Ext.ux.grid.BasicGrid, {

	fTbar: null,	//前置工具栏，让用户自定义的按钮插入在标准工具栏前

	// 组件初始化
	initComponent: function(){
	
		this.tbar = new Ext.Toolbar();
		
		if(this.fTbar && this.fTbar.length > 0){
			for(var i=0, len=this.fTbar.length; i<len; i++){
				this.tbar.addItem(this.fTbar[i]);
			}
		}
	
		this.buildTopToolbar();
		
		this.addEvents('saveSort');
		
		Ext.ux.grid.SortGrid.superclass.initComponent.call(this);
	},
	getRowIndex: function(record){
		var id = record.get(this.keyField);
	    var index = this.store.find(this.keyField, id);
	    return index;
	},
	saveSort: function(){
		var ids = new Array();
		for(var i = 0; i < this.store.getCount(); i++){
			var id = this.store.getAt(i).get(this.keyField);
			ids.push(id);
		}
		this.fireEvent('saveSort', ids);
	},
	goFirst: function(){
		var sel = this.getSelectionModel().getSelections();
		if(sel.length > 0){
			var selectRowIndex = new Array();
			for(var i = 0; i < sel.length; i++){
				var record = sel[i];
					
				var rowIndex = this.getRowIndex(record);
				if(rowIndex != 0){
					this.store.removeAt(rowIndex);
					this.store.insert(i, record);
				}
				selectRowIndex.push(i);
			}
			this.getSelectionModel().selectRows(selectRowIndex);
		}
		else{
			Ext.Msg.alert('系统提示', '请先选择记录')
		}
	},
	upWard: function(){
		var sel = this.getSelectionModel().getSelections();
		if(sel.length > 0){
			var selectRowIndex = new Array();
			for(var i = 0; i < sel.length; i++){
				var record = sel[i];
				var rowIndex = this.getRowIndex(record);
				if(rowIndex != 0){
					this.store.removeAt(rowIndex);
					this.store.insert(rowIndex-1, record);
					selectRowIndex.push(rowIndex-1);
				}
				else
					selectRowIndex.push(0);
				}
				this.getSelectionModel().selectRows(selectRowIndex);
			}
		else{
			Ext.Msg.alert('系统提示', '请先选择记录')
		}
	},
	backWard: function(){
		var sel = this.getSelectionModel().getSelections();
		if(sel.length > 0){
			var selectRowIndex = new Array();
			for(var i = sel.length - 1; i >= 0; i--){
				var record = sel[i];
				var rowIndex = this.getRowIndex(record);
				if(rowIndex != this.store.getCount()-1){
					this.store.removeAt(rowIndex);
					this.store.insert(rowIndex+1, record);
					selectRowIndex.push(rowIndex+1);
				}
				else
					selectRowIndex.push(this.gridPanel.store.getCount()-1);
			}
			this.getSelectionModel().selectRows(selectRowIndex);
		}
		else{
			Ext.Msg.alert('系统提示', '请先选择记录')
		}
	},
	goLast: function(){
		var sel = this.getSelectionModel().getSelections();
		if(sel.length > 0){
			var len = this.store.getCount();
			var selectRowIndex = new Array();
			for(var i = 0; i < sel.length; i++){
				var record = sel[i];
					
				var rowIndex = this.getRowIndex(record);
				if(rowIndex != len - 1){
					this.store.removeAt(rowIndex);
					this.store.insert(len-1, record);
				}
				selectRowIndex.push(len-i-1);
			}
			this.getSelectionModel().selectRows(selectRowIndex);
		}
		else{
			Ext.Msg.alert('系统提示', '请先选择记录')
		}
	},
	buildTopToolbar: function(){
	
		this.tbar.addItem('->');
		
		this.tbar.addItem({
				text : '',
				tooltip : '把所选的列移动最前面',
				iconCls : 'icon-top',
				handler : this.goFirst,
				scope : this
		});
		
		this.tbar.addItem('-');
		
		this.tbar.addItem({
				text : '',
				tooltip : '把所选的列向上移动一个位置',
				iconCls : 'icon-up',
				handler : this.upWard,
				scope : this
		});
		
		this.tbar.addItem('-');
		
		this.tbar.addItem({
				text : '',
				tooltip : '把所选的列向下移动一个位置',
				iconCls : 'icon-down',
				handler : this.backWard,
				scope : this
		});
		
		this.tbar.addItem('-');
		
		this.tbar.addItem({
				text : '',
				tooltip : '把所选的列移动最后面',
				iconCls : 'icon-bottom',
				handler : this.goLast,
				scope : this
		});
		
		this.tbar.addItem('-');
		
		this.tbar.addItem({
				text : '保存排序',
				tooltip : '保存排序的结果',
				iconCls : 'icon-save',
				handler : this.saveSort,
				scope : this
		});
		
		this.tbar.addItem('-');
	}
});

Ext.reg('sortGrid', Ext.ux.grid.SortGrid);